﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Text;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 友情连接管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PeerReviewScoreController : Controller
    {
        //
        // GET: /Admin/PeerReviewScore/

        public ActionResult Index()
        {

            if (OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) != null && OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) != "")
            {
                BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
                List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();
                if (PeerReviewModel.Count > 0)
                {
                    if (PeerReviewModel[0].FStatus == "B" && PeerReviewModel[0].FBeginDate < DateTime.Now)
                    {
                        if (PeerReviewModel[0].FEndDate > DateTime.Now)
                        {

                            BLL.PeerReviewVoter PeerReviewVoterBLL = new BLL.PeerReviewVoter();
                            List<Model.PeerReviewVoter> PeerReviewVoterList = PeerReviewVoterBLL.GetModelList(" FPeerReviewID = '" + PeerReviewModel[0].FPeerReviewID + "' and FVoterEmplNo='" + OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) + "'");

                            if (PeerReviewVoterList.Count > 0)
                            {

                                if (PeerReviewModel[0].FIsVoteIPControl == "N")
                                {
                                    BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                                    List<Model.PeerReviewResult> peerReviewResultList = PeerReviewResultBLL.GetModelList(" FVoterEmplNo ='" + OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) + "'");
                                    if (peerReviewResultList.Count > 0)
                                    {
                                        ViewData["table"] = "";// "<div><font color='red>您已进行过评议，无法再次评议</font></div>";
                                        ViewData["jsAlert"] = "您已进行过评议，无法再次评议";
                                    }
                                    else
                                    {
                                        ViewData["table"] = GetTable();
                                        ViewData["title"] = PeerReviewModel[0].FTitle;
                                    }
                                }
                                else
                                {
                                    BLL.PeerReviewIP PeerReviewIPBLL = new BLL.PeerReviewIP();
                                    List<Model.PeerReviewIP> PeerReviewIPList = PeerReviewIPBLL.GetListPeerReviewIP(PeerReviewModel[0].FPeerReviewID);

                                    List<Model.PeerReviewIP> selectPeerReviewIPList = (from nm in PeerReviewIPList where nm.FProp == "1" select nm).ToList();


                                    string ipList = "";
                                    foreach (Model.PeerReviewIP ipModel in selectPeerReviewIPList)
                                    {
                                        if (ipList != "")
                                        {
                                            ipList += "," + ipModel.FIPGroupID.ToString();
                                        }
                                        else
                                        {
                                            ipList += ipModel.FIPGroupID.ToString();
                                        }
                                    }
                                    if (Fetch.IpCheck("1", ipList))
                                    {
                                        BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                                        List<Model.PeerReviewResult> peerReviewResultList = PeerReviewResultBLL.GetModelList(" FVoterEmplNo ='" + OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) + "'");
                                        if (peerReviewResultList.Count > 0)
                                        {
                                            ViewData["table"] = "";// "<div><font color='red>您已进行过评议，无法再次评议</font></div>";
                                            ViewData["jsAlert"] = "您已进行过评议，无法再次评议";
                                        }
                                        else
                                        {
                                            ViewData["table"] = GetTable();
                                            ViewData["title"] = PeerReviewModel[0].FTitle;
                                        }
                                    }
                                    else
                                    {
                                        ViewData["table"] = "";
                                        ViewData["jsAlert"] = "IP受限制不允许访问";

                                    }
                                }

                            }
                            else
                            {
                                ViewData["table"] = "";
                                ViewData["jsAlert"] = "您没有权限进行评议";
                            }
                        }
                        else
                        {
                            ViewData["table"] = "";
                            ViewData["jsAlert"] = "评议时间已到！";
                        }
                    }
                    else
                    {
                        ViewData["table"] = "";
                        ViewData["jsAlert"] = "评议未开始或评议已结束";
                    }
                }
                else
                {
                    ViewData["table"] = "";
                    ViewData["jsAlert"] = "评议未开始或评议已结束";
                }
            }
            else
            {
                ViewData["table"] = "";
                Response.Write("<script>alert('请先登录');window.parent.location.href='/ElingOA6_Admin_DSJCJ/Login?url=/ElingOA6_Admin_DSJCJ/Admin/PeerReviewScore';</script>");
                Response.Flush();
                Response.End();

            }

            return View();
        }

        public string GetTable()
        {
            BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            StringBuilder str = new StringBuilder();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();

            if (PeerReviewModel.Count > 0)
            {
                BLL.PeerReviewRule PeerReviewRuleBLL = new BLL.PeerReviewRule();
                List<Model.PeerReviewRule>  PeerReviewRuleList = PeerReviewRuleBLL.GetModelList(" FPeerReviewID = '" + PeerReviewModel[0].FPeerReviewID + "'");

                BLL.PeerReviewRuleOption PeerReviewRuleOptionBLL = new BLL.PeerReviewRuleOption();
                List<Model.PeerReviewRuleOption> PeerReviewRuleOptionList = PeerReviewRuleOptionBLL.GetModelList("");
                if (PeerReviewModel[0].FIsSelectAll == "Y")
                {
                    ViewData["xxList"] = PeerReviewRuleOptionList;
                }

                BLL.PeerReviewBeenVoterCls PeerReviewBeenVoterClsBLL = new BLL.PeerReviewBeenVoterCls();
                List<Model.PeerReviewBeenVoterCls> PeerReviewBeenVoterClsList = PeerReviewBeenVoterClsBLL.GetAll();

                BLL.PeerReviewBeenVoter PeerReviewBeenVoterBLL = new BLL.PeerReviewBeenVoter();
                List<Model.PeerReviewBeenVoter> PeerReviewBeenVoterList = PeerReviewBeenVoterBLL.GetAllList("");

                BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                List<Model.PeerReviewResult> peerReviewResultList = PeerReviewResultBLL.GetModelList(" FVoterEmplNo ='" + OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO) + "'");
                //str.Append("<div class='FixedTitleRow'>");
                str.Append("<table  border=\"0\" cellpadding=\"5\" cellspacing=\"0\"  class=\"tableForm\">");

                str.Append("<tr>");
                str.Append("<td class='tdClass' style=' text-align:center;width:60px ;color:#fff; font-weight:bold; background:url(" + Url.Content("~/Content/images/titlebg2.jpg") + ") repeat-x'>类别");
                str.Append("</td>");
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    str.Append("<td style='text-align:center;color:#fff; font-weight:bold; background:url(" + Url.Content("~/Content/images/titlebg2.jpg") + ") repeat-x' class='tdClass'  colspan='" + PeerReviewRuleOptionList.Count + "'>");
                    str.Append(column.FRuleCls);
                    str.Append("</td>");
                }
                str.Append("</tr>");
                str.Append("<tr>");
                str.Append("<td style='text-align:center' class='tdClass'  bgcolor='e0f2ff' style='width:60px'>要素");
                str.Append("</td>");
                int i = 1;
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    if (i % 2 != 0)
                    {
                        str.Append("<td style='text-align:left' class='tdClass' valign='top' colspan='" + PeerReviewRuleOptionList.Count + "'>");
                        str.Append(column.FRuleNote);
                        str.Append("</td>");
                    }
                    else
                    {
                        str.Append("<td style='text-align:left' bgcolor='e0f2ff' class='tdClass' valign='top' colspan='" + PeerReviewRuleOptionList.Count + "'>");
                        str.Append(column.FRuleNote);
                        str.Append("</td>");
                    }
                    i++;
                }
                str.Append("</tr>");
                str.Append("<tr>");
                str.Append("<td style='text-align:center'class='tdClass'  bgcolor='e0f2ff'  nowrap>姓名");
                str.Append("</td>");
                i = 1;
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    if (i % 2 != 0)
                    {
                        foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                        {
                            str.Append("<td align='center' class='tdClass'  nowrap>");
                            str.Append(rowColum.FScoreName);
                            str.Append("</td>");
                        }
                    }
                    else
                    {
                        foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                        {
                            str.Append("<td align='center' bgcolor='e0f2ff' class='tdClass'  nowrap>");
                            str.Append(rowColum.FScoreName);
                            str.Append("</td>");
                        }
                    }
                    i++;
                }
                str.Append("</tr>");

                //str.Append("</table>");
                //str.Append("</div>");
                //str.Append("<table  border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  class=\"tableForm\">");

                foreach (Model.PeerReviewBeenVoterCls cls in PeerReviewBeenVoterClsList)
                {
                    str.Append("<tr>");
                    str.Append("<td style='text-align:center'   bgcolor='e0f2ff' class='tdClass' colspan='" + (PeerReviewRuleList.Count * PeerReviewRuleOptionList.Count + 1) + "' >");
                    str.Append(cls.FBeenVoterClsName);
                    str.Append("</td>");
                    str.Append("</tr>");
                    List<Model.PeerReviewBeenVoter> selectPeerReviewBeenVoterList = (from nm in PeerReviewBeenVoterList where nm.FBeenVoterClsCd == cls.FBeenVoterClsCd select nm).ToList();
                    foreach (Model.PeerReviewBeenVoter row in selectPeerReviewBeenVoterList)
                    {
                        str.Append("<tr  onmouseover=\"this.style.backgroundColor='#e0f2ff'\" onmouseout=\"this.style.backgroundColor='#ffffff'\">");
                        str.Append("<td align='center'  bgcolor='e0f2ff' class='tdClass'  nowrap>");
                        str.Append(row.FEmpName);
                        str.Append("</td>");
                        i = 1;
                        foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                        {
                            if (i % 2 != 0)
                            {
                                foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                                {
                                    List<Model.PeerReviewResult> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo && nm.FBeenVoterDeptNo == row.FBeenVoterDeptNo && nm.FPeerReviewID == PeerReviewModel[0].FPeerReviewID && nm.FPeerReviewRuleCd == column.FPeerReviewRuleCd && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();

                                    if (select.Count > 0)
                                    {
                                        str.Append("<td align='center' class='tdClass'  title='" + column.FRuleCls + ":" + rowColum.FScoreName + "'>");
                                        str.Append("<input  type='radio' checked='checked' name='" + (column.FPeerReviewRuleCd + row.FBeenVoterEmplNo) + "' id='cs1' value='" + rowColum.FPeerReviewRuleScoreCd + "' title='" +column.FRuleCls +":"+ rowColum.FScoreName + "' />");
                                        str.Append("</td>");
                                    }
                                    else
                                    {
                                        str.Append("<td align='center' class='tdClass'  title='" + column.FRuleCls + ":" + rowColum.FScoreName + "'>");
                                        str.Append("<input  type='radio' name='" + (column.FPeerReviewRuleCd + row.FBeenVoterEmplNo) + "' id='cs1' value='" + rowColum.FPeerReviewRuleScoreCd + "' title='" + column.FRuleCls + ":" + rowColum.FScoreName + "' />");
                                        str.Append("</td>");
                                    }
                                }
                            }
                            else
                            {
                                foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                                {
                                    List<Model.PeerReviewResult> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo && nm.FBeenVoterDeptNo == row.FBeenVoterDeptNo && nm.FPeerReviewID == PeerReviewModel[0].FPeerReviewID && nm.FPeerReviewRuleCd == column.FPeerReviewRuleCd && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();

                                    if (select.Count > 0)
                                    {
                                        str.Append("<td align='center' bgcolor='e0f2ff' class='tdClass'  title='" + column.FRuleCls + ":" + rowColum.FScoreName + "'>");
                                        str.Append("<input  type='radio' checked='checked' name='" + (column.FPeerReviewRuleCd + row.FBeenVoterEmplNo) + "' id='cs1' value='" + rowColum.FPeerReviewRuleScoreCd + "' title='" + column.FRuleCls + ":" + rowColum.FScoreName + "' />");
                                        str.Append("</td>");
                                    }
                                    else
                                    {
                                        str.Append("<td align='center' bgcolor='e0f2ff' class='tdClass'  title='" + column.FRuleCls + ":" + rowColum.FScoreName + "'>");
                                        str.Append("<input  type='radio' name='" + (column.FPeerReviewRuleCd + row.FBeenVoterEmplNo) + "' id='cs1' value='" + rowColum.FPeerReviewRuleScoreCd + "' title='" + column.FRuleCls + ":" + rowColum.FScoreName + "' />");
                                        str.Append("</td>");
                                    }
                                }
                            }
                            i++;
                        }
                        str.Append("</tr>");
                    }
                }
                str.Append("</table>");
            }

            return str.ToString();
        }

        public ActionResult Save() 
        {
            BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();

            BLL.PeerReviewRule PeerReviewRuleBLL = new BLL.PeerReviewRule();
            List<Model.PeerReviewRule> PeerReviewRuleList = PeerReviewRuleBLL.GetModelList(" FPeerReviewID = '" + PeerReviewModel[0].FPeerReviewID + "'");

            BLL.PeerReviewBeenVoter PeerReviewBeenVoterBLL = new BLL.PeerReviewBeenVoter();
            List<Model.PeerReviewBeenVoter> PeerReviewBeenVoterList = PeerReviewBeenVoterBLL.GetAllList("");

            List<Model.PeerReviewResult> PeerReviewResultList = new List<Model.PeerReviewResult>();
            foreach (Model.PeerReviewRule column in PeerReviewRuleList)
            {
                foreach (Model.PeerReviewBeenVoter rowColum in PeerReviewBeenVoterList)
                {
                    Model.PeerReviewResult PeerReviewResultModel = new Model.PeerReviewResult();
                    PeerReviewResultModel.FBeenVoterClsCd = rowColum.FBeenVoterClsCd;
                    PeerReviewResultModel.FBeenVoterDeptNo = rowColum.FBeenVoterDeptNo;
                    PeerReviewResultModel.FBeenVoterEmplNo = rowColum.FBeenVoterEmplNo;
                    PeerReviewResultModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                    PeerReviewResultModel.FPeerReviewID = PeerReviewModel[0].FPeerReviewID;
                    PeerReviewResultModel.FPeerReviewRuleCd = column.FPeerReviewRuleCd;
                    PeerReviewResultModel.FPeerReviewRuleScoreCd = Fetch.Post(column.FPeerReviewRuleCd + rowColum.FBeenVoterEmplNo);
                    PeerReviewResultModel.FVoterDeptNo = OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO);
                    PeerReviewResultModel.FVoterEmplNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                    PeerReviewResultList.Add(PeerReviewResultModel);
                }
            }

            Model.PeerReviewVoter PeerReviewVoterModel = new Model.PeerReviewVoter();
            PeerReviewVoterModel.FStatus = "Y";
            PeerReviewVoterModel.FVoteDate = DateTime.Now;
            PeerReviewVoterModel.FVoteIP = Common.GenerateUtil.GetClientIPv4Address();
            PeerReviewVoterModel.FVoterEmplNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
            PeerReviewVoterModel.FPeerReviewID = PeerReviewModel[0].FPeerReviewID;
            PeerReviewVoterModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
            PeerReviewVoterModel.FVoterDeptNo = OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO);

            try
            {
                BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                PeerReviewResultBLL.Add(PeerReviewResultList, PeerReviewVoterModel);
                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }

           
            ViewData["table"] = "";
            return View("Index");
        }

    }
}
